package com.tf_cut.admin.person.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;
import com.tf_cut.admin.person.vo.PersonAndAccountVo;
import com.tf_cut.admin.person.vo.PersonBillAndConsigneeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/person/info")
@Slf4j
public class PersonInfoController {

    @Autowired
    private BackEndApi personInfoApi;

    // 进入商户清单页面
    @GetMapping("list")
    public ModelAndView sellerList() {
        return new ModelAndView("person/info/list", "personInfo", null);
    }

    // 进入新增商户页面
    @GetMapping("sellerinsert")
    public ModelAndView sellerInsert() {
        return new ModelAndView("seller/sellerinsert", "seller", null);
    }
    
    // 进入个人认证驳回页面
    @GetMapping("personInforefuse")
    public ModelAndView personInforefuse(String personId) {
        return new ModelAndView("person/info/personInforefuse", "personId", personId);
    }

    // 进入新增商户页面
    @GetMapping("sellerimport")
    public ModelAndView sellerImport() {
        return new ModelAndView("seller/sellerimport", "seller", null);
    }

    /**
     * 查询个人认证列表
     *
     * @param personAndAccountVo
     * @return
     */
    @PostMapping(value = "list/page")
    public Result<PageInfo<PersonBillAndConsigneeVo>> queryPersonBillList(PersonAndAccountVo personAndAccountVo) {
        String json = JSON.toJSONString(personAndAccountVo);
        json = personInfoApi.queryPersonInfoList(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }

    @PostMapping("dealPersonInfo")
    public Result<PersonAndAccountVo> dealSellerInfo(PersonAndAccountVo avo) {
        String rtnJson;
        Result<PersonAndAccountVo> result;
        if(StringUtils.isNoneBlank(avo.getCardStatus())) {
            try {
                rtnJson = personInfoApi.updatepersonmobileInfo(JSON.toJSONString(avo));
                result = JSON.parseObject(rtnJson, Result.class);
            } catch (Exception e) {
                log.error(e.getMessage(),e);
                avo.setPersonId(null);
                result = Result.of(avo);
                result.setFlag(false);
            }
        }else {
            Result<PersonAndAccountVo> result2 = Result.of(null);
            result2.setMsg("操作参数必传！");
            result2.setFlag(false);
            return result2;
        }
        return result;
    }

}
